package com.bigfish.crm.service;

import java.util.Date;
import java.util.List;
import java.util.Random;

import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bigfish.common.utils.PageItem;
import com.bigfish.crm.dao.MemberDao;
import com.bigfish.crm.dto.Member;

@Service
public class MemberService {
	//@KSession("member_ksession")
	private KieSession kSession;
	
	@Autowired
	private MyKieService kieService;
	
	@Autowired
	private MemberDao memberDao;
	
	public List<Member> getPageMember(PageItem page){
		List<Member> memberList=memberDao.getMemberList(page);
		//kSession=SpringContextHolder.getBean(KieSession.class);
		long a=System.currentTimeMillis();
		kSession=kieService.getKieSession();
		long b=System.currentTimeMillis();
		System.out.println("获取session耗费的时间为:"+(b-a));
		for(int i=0;i<memberList.size();i++){
			kSession.insert(memberList.get(i));
		}
		/*QueryResults qr=kSession.getQueryResults("memberNum");
		System.out.println("工作内存中现有的member数量为:"+qr.size());*/
		kSession.fireAllRules();
		long c=System.currentTimeMillis();
		System.out.println("执行规则耗费的时间为:"+(c-b));
		kSession.dispose();
		System.out.println("回收资源耗费的时间为:"+(System.currentTimeMillis()-c));
		//kSession.execute(CommandFactory)
		return memberList;
		
	}
	public Member getMember(){
		Member member=new Member();
		member.setUserId(System.currentTimeMillis()+"");
		member.setUserName(System.currentTimeMillis()+"");
		member.setBuySumMoney(new Random().nextInt(20000));
		member.setLatestOrderTime(new Date(System.currentTimeMillis()-new Random().nextInt(100000000)));
		return member;
		
	}
}
